package com.fc.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.fc.entity.User;
import com.fc.service.UserBiz;
import net.sf.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class UserAction extends ActionSupport{

	private String name;
	private String password;
	private String message;

    private JSONObject result;
	
	// 定义业务接口属性
	private UserBiz userBiz;
	//定义setter方法,方法名应和Spring配置文件中业务Bean的名称相匹配
	public void setUserBiz(UserBiz userBiz) {
		this.userBiz = userBiz;
	}

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}	

	public String login(){
		
		User user = userBiz.login(name, password);
		ActionContext ac = ActionContext.getContext();
		if(name == null || name.equals("")){
			this.setMessage("登录失败，请检查用户名和密码是否正确");
			return "login_input";
		}
		if(password == null || password.equals("")){
			this.setMessage("登录失败，请检查用户名和密码是否正确");
			return "login_input";
		}		
		Map session = ac.getSession();
		if(null != user){
			session.put("login", user);				
			return "login_success";
		}
		
		this.setMessage("登录失败，请检查用户名和密码是否正确");
		
		return "login_input";
	}

	public String validName(){
		boolean flag= false;
		String msg = "";
		if("admin".equals(name)){
			msg = "<font color='green'>账号可用</font>";
			flag = true;
		}else{
			msg = "<font color='red'>账号不存在！</font>";
		}

		Map<String,Object> list = new HashMap<String,Object>();
		list.put("flag", flag);
		list.put("msg",msg);
		list.put("password",null);
		if(flag){
			list.put("name",name);
		}

		result = JSONObject.fromObject(list);

		return "success";
	}

	public JSONObject getResult() {
		return result;
	}

	public void setResult(JSONObject result) {
		this.result = result;
	}
}
